#!/usr/bin/perl
use strict;
use POSIX 'strftime';
use JSON;
use Data::Dumper;

my $time = time - 86400 * 365 * 4 - 86400;
my $i=0;
for ($i=0;$time < time; $i++) {
	$time = $time + 86400;
	my $date = strftime("%Y-%m-%d", localtime($time));
	my $file = "../data/$date.txt";
	my $content = `cat $file`;
	$content = substr($content, 21, length($content));
	chop($content);
	chop($content);
	$content =~ s/\'/\"/g;
	#print "content=$content\n";
	my $json  = JSON->new();
	my $perl_scalar = $json->decode($content);
	my $financeData = $perl_scalar->{'financeData'};
	#print "perl_scalar=\n", Dumper($financeData);
	my ($str_date, $str_time, $str_event, $str_level, $str_before, $str_expect, $str_after);
	foreach my $event_hash (@$financeData) {
		$str_date =  $event_hash->{'date'};
		$str_time  =  $event_hash->{'time'};
		$str_event  =  $event_hash->{'affair'};
		$str_level  =  $event_hash->{'importance'};
		$str_before  =  $event_hash->{'beforeData'};
		$str_expect  =  $event_hash->{'forecast'};
		$str_after =  $event_hash->{'result'};

		if (length($str_before)<1) {$str_before='无数据';}
		if (length($str_expect)<1) {$str_expect='无数据';}
		if (length($str_after)<1) {$str_expect='无数据';}
		if (length($str_time)<1) {$str_time='notime';}

		if ($str_before =~ /nbsp/) {$str_before='侦查中';}
		if ($str_expect =~ /nbsp/) {$str_expect='侦查中';}
		if ($str_after =~ /nbsp/) {$str_after='侦查中';}

		if (length($str_date)<1 
				or length($str_time)<1 
				or length($str_event)<1 
				or length($str_level)<1 
				or length($str_before)<1 
				or  length($str_expect)<1 
				or length($str_after)<1) {
			#print "error date=$str_date: $str_date $str_time $str_event $str_level $str_before $str_expect $str_after\n";
		} else {
			#print "right date=$str_date: $str_date $str_time $str_event $str_level $str_before $str_expect $str_after\n";
			print "mysql -hlocalhost -uroot -p123456 d_gold -e \"insert into t_daily_news (c_date,c_time,c_event,c_level,c_before,c_expect,c_after,resInt1,createTime) values (\'$str_date\', \'$str_time\', \'$str_event\', \'$str_level\', \'$str_before\', \'$str_expect\', \'$str_after\',1,now())\"\n";
		}
	}
}





